var tempChart;

function initEChart() {
    // 基于准备好的dom，初始化ECharts实例
    tempChart = echarts.init(document.getElementById('temp-charts'));

    // 指定图表的配置项和数据
    var option = {
        title: {

        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            show: false
        },
        calculable: true,
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: []
        },
        yAxis: {
            type: 'value',
            min: 35,
            max: 38,
            interval: 0.2,
            nameGap: 5,
            boundaryGap: false,
            splitLine: {
                lineStyle: { type:'dashed', opacity: 0.5}
            }
        },
        series: [{
            type: 'line',
            name: '体温',
            data: [],
            markLine: {
                data: [{
                    name: '平均体温',
                    type: 'average'
                }]
            },
            markPoint: {
                data: [{
                    type: 'max',
                    name: '最高'
                },
                    {
                        type: 'min',
                        name: '最低'
                    }]
            }

        }],
        toolbox: {
            show: true,
            feature: {
                dataView: {readOnly: true},
                magicType: {
                    show: true,
                    type: ['line', 'bar']
                },
                restore: {show: true}
            }
        }
    };

    // 使用刚指定的配置项和数据显示图表。
    tempChart.setOption(option);
    tempChart.showLoading({text: '正在加载体温数据...'});
}

var barItemOn = 'weui-bar__item_on';
var tabItemAct = 'weui-tab__bd-item--active';

$(function(){
    $('#addDate').val(getNowFormatDate());
    $("#addDate").calendar();

    var w = $(window).width();
    var h = $(window).height();

    $('#temp-charts').css({'width':w, 'height':h * 0.8});
    initEChart();
    getData('chart');

    $('.weui-tabbar__item').on('click', function () {
        $(this).find('.icon-font').addClass('icon-font-color');
        $(this).siblings('.'+barItemOn).find('.icon-font').removeClass('icon-font-color');
        $(this).addClass(barItemOn).siblings('.'+barItemOn).removeClass(barItemOn);
        var id = this.id.substr(this.id.length-1);
        $('#tab'+id).addClass(tabItemAct).siblings('.'+tabItemAct).removeClass(tabItemAct)
    });

    $('#addNewTemp').on('click', function () {
        var date = $('#addDate').val();
        var temp = $('#addTemp').val();
        if (!date) {
            $.toptip('亲爱的,你是在逗我嘛,选个日期好啦!');
            return;
        } else if(!temp) {
            $.toptip('别偷懒,要记得量体温呢!');
            return;
        }

        $.showLoading();
        postData(0, 'add', date, temp);
    });
});

function postData(id, option, date, temp) {
    $.post('temp-monitor/addTempData.do',
        {'temp_id':id, 'option':option, 'measure_date':date, 'temperature':temp},
        function (data) {
            $.hideLoading();
            getData('chart');
            if (data.code==200) {
                $.toast("添加新体温记录成功!");
                getData('chart');
                $('#addDate').val(getNowFormatDate());
                $('#addTemp').val('');
            } else if (data.code==201) {
                $.confirm({
                    title: '闹心',
                    text: '号外号外,请注意!已经存在有体温记录,确定要覆盖么? :(',
                    onOK: function () {
                        postData(data.tmpId, 'update', date, temp);
                    },
                    onCancel: function () {
                        $.toast("好嘛，咱这回不记录!", "cancel");
                    }
                });
            } else {
                $.toast("添加新体温记录失败!", "cancel");
            }
        },'json');
}

function getData(drawType) {
    $.post('temp-monitor/getTempData.do', {type:drawType}, function(data){
        if (drawType == 'chart') {
            graphicChart(data);
        } else if (drawType == 'table') {
            graphicTable(data);
        }
    }, 'json');
}

function graphicChart(data) {
    tempChart.hideLoading();
    tempChart.setOption({
        xAxis: {
            data: data.date
        },
        series: [{
            name: '体温',
            data: data.temp
        }]
    });
}

function graphicTable(data) {

}

function getNowFormatDate() {
    var now = new Date();
    var sep = "-";
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    if (m >= 1 && m <= 9) {
        m = "0" + m;
    }
    if (d >= 0 && d <= 9) {
        d = "0" + d;
    }
    var curDate = y + sep + m + sep + d;
    return curDate;
}